package com.heima.estatemanagement.controller;

import com.github.pagehelper.Page;
import com.heima.estatemanagement.common.MessageConstant;
import com.heima.estatemanagement.common.PageResult;
import com.heima.estatemanagement.common.Result;
import com.heima.estatemanagement.common.StatusCode;
import com.heima.estatemanagement.domain.ParkingCode;
import com.heima.estatemanagement.domain.TbParking;
import com.heima.estatemanagement.service.ParkingCodeService;
import com.heima.estatemanagement.service.TbParkingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * @Auth: zhuan
 * @Desc: 小区管理控制层:只负责接收前端浏览器发送的请求和请求参数，调用service层获取业务逻辑加工处理后的数据
 */
@RestController
@RequestMapping("/parking")
public class ParkingController {

    @Autowired
    private TbParkingService parkingService;
    @Autowired
    private ParkingCodeService parkingCodeService;

    @RequestMapping("/find")
    public Result find(){
        List<TbParking> all = parkingService.findAll();
        return new Result(false,0,"请求成功adasdasdas",all);
    }

    @RequestMapping("/search")
    public PageResult search(@RequestBody Map searchMap){
        Page<TbParking> page = parkingService.search(searchMap);
        return new PageResult(true, StatusCode.OkLAYUI, MessageConstant.TB_PARKING_SEARCH_SUCCESS,page.getResult(),page.getTotal(),page.getPageSize());
    }

    @RequestMapping("/add")
    public Result add(TbParking parking){
        parkingCodeService.add();
        parking.setCode("XY"+parkingCodeService.findByCode());
        int cn = parkingService.add(parking);
        return new Result(true,StatusCode.OK,MessageConstant.TB_PARKING_ADD_SUCCESS);
    }
    @RequestMapping("/findById")
    public Result findById(int id){
        TbParking parking = parkingService.findById(id);
        System.out.println("parking====>"+parking);
        return new Result(true,StatusCode.OK,MessageConstant.COMMUNITY_FIND_BY_ID_SUCCESS,parking);
    }

    @RequestMapping("/update")
    public Result update(@RequestBody TbParking parking){
         parkingService.update(parking);
        return new Result(true,StatusCode.OK,MessageConstant.COMMUNITY_UPDATE_SUCCESS);
    }


    @RequestMapping("/del")
    public Result del(@RequestBody List<Integer> ids){
        Boolean flag = parkingService.del(ids);
        return new Result(true,StatusCode.OK,MessageConstant.TB_PARKING_DELETE_SUCCESS);
    }

    @RequestMapping("/delById")
    public Result delById(int id){
        if (parkingService.delFindByStatus(id)){
            Boolean flag = parkingService.delById(id);
            return new Result(true,StatusCode.OK,MessageConstant.TB_PARKING_DELETE_SUCCESS);
        }else{
            return new Result(true,StatusCode.ERROR,MessageConstant.TB_PARKING_DELETE_ERROR);
        }
    }
}
